package cn.xi.service.checkItem;

import cn.xi.checkItem.OrderService;
import cn.xi.constant.MessageConstant;
import cn.xi.dao.MemberDao;
import cn.xi.dao.OrderDao;
import cn.xi.dao.OrdersettingDao;
import cn.xi.entity.Result;
import cn.xi.pojo.Member;
import cn.xi.pojo.Order;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result add(Map map) {
        //检查用户所选择的预约日期是否已经提前经行了预约设置,如果没有设置则无法预约
        String orderDate = (String) map.get("orderDate");
        Integer selectid = ordersettingDao.selectid(orderDate);
        if (selectid == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查用户所选择的预约日期是否已经约满,如果已经约满则无法预约
        Integer selectnumber = ordersettingDao.selectnumber(orderDate);
        if (selectnumber == null) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        // 检查当前用户是否为会员,如果不是会员则自动添加
        String idCard = (String) map.get("idCard");
        Integer integer = memberDao.selectidCard(idCard);
        if (integer == null) {
            Member m = new Member();
            m.setName((String) map.get("name"));
            m.setSex((String) map.get("sex"));
            m.setPhoneNumber((String) map.get("telephone"));
            m.setIdCard((String) map.get("idCard"));
            try {
                memberDao.save(m);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
            }
            integer = memberDao.getid();
        }
        //检查用户是否重复预约(同一个用户同一天预约了同一个套餐),如果是重复预约则无法完成再次预约
        String setmealId = (String) map.get("setmealId");
        Integer smid = Integer.parseInt(setmealId);
        Integer intMember = orderDao.selectMember(integer, orderDate, smid);
        if (intMember != 0) {
            return new Result(false, MessageConstant.HAS_ORDERED);
        }
        //并完成预约
        Order order = new Order();
        order.setMemberId(integer);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(orderDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setOrderDate(parse);
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(smid);
        try {
            orderDao.save(order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        Integer getid = memberDao.getid();
        //预约成功,更新当日预约人数
        Integer selectreservations = ordersettingDao.selectreservations(orderDate);
        if (selectreservations == null) {
            ordersettingDao.update_reservations(orderDate);
        } else {
            ordersettingDao.updatereservations(orderDate);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, getid);
    }

    @Override
    public Order findById(Integer id) {
        Order byId = orderDao.findById(id);
        Date orderDate = byId.getOrderDate();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
        byId.setOrderDateString(format);
        return byId;
    }
}
